/**
 * 手机号码处理工具类
 *
 * @author 顾章华
 */

/**
 *  手机号码按照 344 的方式进行格式化显示
 *
 * @example
 *  1.
 *    mobileFormat(`18915601235`) => `189 1560 1235`
 *  2.
 *    mobileFormat(`18915601235` , ',') => `189,1560,1235`
 *
 * @param {String} mobile   手机号码
 * @param {String} symbol   分割符号 默认为 ' '
 */
export const mobileFormat = (mobile, symbol = ' ') => {
  let value = mobile.replace(/\s+/g, '');
  let valueLen = value.length;

  if (valueLen > 3 && valueLen < 8) {
    value = `${value.substr(0, 3)}${symbol}${value.substr(3)}`;
  } else if (valueLen >= 8) {
    value = `${value.substr(0, 3)}${symbol}${value.substr(
      3,
      4
    )}${symbol}${value.substr(7)}`;
  }
  return value;
};
/**
 * 手机号码部分隐藏工具方法
 *
 *  18915601235 =>   189*****35
 *
 *  @example phoneFormat('18915601235' , 3, 2, '*')
 *
 * @param {Number} mobileNumber         手机号码
 * @param {Number} leftNoEllipsisNumber  左边不始隐藏的个数
 * @param {Number} rightNoEllipsisNumber 右边不隐藏的个数
 * @param {String} ellipsisSeparator   隐藏的符号
 */
export const mobileEllipsisFormat = (
  mobileNumber,
  leftNoEllipsisNumber = 3,
  rightNoEllipsisNumber = 2,
  ellipsisSeparator = '*'
) => {
  //  获取手机号的个数
  var strLength = mobileNumber.length;
  // 不能左边加右边的数目大于总个数
  if (leftNoEllipsisNumber + rightNoEllipsisNumber >= strLength) {
    leftNoEllipsisNumber = 0;
    rightNoEllipsisNumber = 0;
  }
  var star = '';
  var strRel = '';
  //星号部分
  var hideSec = mobileNumber.substring(
    leftNoEllipsisNumber,
    strLength - rightNoEllipsisNumber
  );
  for (var i = 0; i < hideSec.length; i++) {
    star += ellipsisSeparator;
  }
  strRel =
    mobileNumber.substring(0, leftNoEllipsisNumber) +
    star +
    mobileNumber.substr(strLength - rightNoEllipsisNumber);
  return strRel;
};
